package com.wfcm.controller;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.wfcm.entity.*;
import com.wfcm.service.*;
import com.wfcm.utils.ISsoLoginHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wfcm.annotation.IgnoreToken;
import com.wfcm.utils.R;


@Controller
@RequestMapping("/ad")
@ResponseBody
public class AdInfoController {
    private Logger log = LoggerFactory.getLogger(AdInfoController.class);
	
    @Autowired
    private WfAdInfoService wfAdInfoService;
    @Autowired
	private WfAppVersionService appVersionService;
	@Autowired
	private WfAppImageService appImageService;
	@Autowired
	private WfActivityNewsService wfActivityNewsService;
	@Autowired
	private WfNewMessageService newMessageService;
	@Autowired
	private WfIconService iconService;

    /*@RequestMapping("/findAdInfo")
    @IgnoreToken
	public R findAdInfo() {
		
		List<WfAdInfoEntity> adList = wfAdInfoService.queryList(null);
		return R.ok().put("adList", adList);
		
	}*/

	@RequestMapping("/findAdInfo")
	@IgnoreToken
	public R findAdInfo(HttpServletRequest req) {
        List<WfAdInfoEntity> adList = wfAdInfoService.queryList(null);
        try {
            // 展示控制 1 (ios) 2 (android) 3 (h5)
            Map<Integer, List<Integer>> map = wfAdInfoService.displayControl();
            String userAgent = req.getHeader("user-agent");
            if (userAgent.contains("iOS") && map.get(1).size() > 0) {
                for (int i = 0; i < adList.size(); i++) {
                    if (map.get(1).contains(adList.get(i).getId())) {
                        adList.remove(i);
                    }
                }
            } else if (userAgent.contains("okhttp") && map.get(2).size() > 0) {
                for (int i = 0; i < adList.size(); i++) {
                    if (map.get(2).contains(adList.get(i).getId())) {
                        adList.remove(i);
                    }
                }
            } else if (map.get(3).size() > 0) {
                for (int i = 0; i < adList.size(); i++) {
                    if (map.get(3).contains(adList.get(i).getId())) {
                        adList.remove(i);
                    }
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return R.ok().put("adList", adList);
	}

	@RequestMapping("/findIndex")
	@IgnoreToken
	@ResponseBody
	public R findIndex(HttpServletRequest req) {
		R r = R.ok();
		List<WfAdInfoEntity> adList = wfAdInfoService.queryList(null);
		try {
			// 展示控制 1 (ios) 2 (android) 3 (h5)
			//Map<Integer, List<Integer>> map = wfAdInfoService.displayControl();
			String userAgent = req.getHeader("user-agent");
			if (userAgent.contains("iOS") && adList.size() > 0) {
				for (int i = 0; i < adList.size(); i++) {
					if (null!=adList.get(i).getNoClient() && adList.get(i).getNoClient().contains("1")) {
						adList.remove(i);
						i=-1;
					}
				}
			} else if (userAgent.contains("okhttp") && adList.size() > 0) {
				for (int i = 0; i < adList.size(); i++) {
					if (null!=adList.get(i).getNoClient() && adList.get(i).getNoClient().contains("2")) {
						adList.remove(i);
						i=-1;
					}
				}
			} else if (adList.size() > 0) {
				for (int i = 0; i < adList.size(); i++) {
					if (null!=adList.get(i).getNoClient() && adList.get(i).getNoClient().contains("3")) {
						adList.remove(i);
						i=-1;
					}
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}

		List<WfActivityNewsEntity> newsList = wfActivityNewsService.queryList(null);

		List<WfIconEntity> iconList = iconService.queryList(null);

		Integer userId = ISsoLoginHelper.confirm(req);
		if(userId != null && 0 != newMessageService.newFlag(userId)){
			r.put("newFlag",1);//有新消息
		}else {
			r.put("newFlag",0);
		}
		r.put("adList",adList);
		r.put("iconList",iconList);
		r.put("newsList",newsList);
		return r;
	}

	@RequestMapping("/androidVersion")
    @IgnoreToken
	@ResponseBody
	public R androidVersion(HttpServletRequest req) {
		
    	String type = req.getParameter("type");//1/ios  2/安卓
    	WfAppVersionEntity appVersion = appVersionService.queryObject(Integer.valueOf(type));
    	R r = new R();
    	r.put("version",  appVersion.getVersion());
    	r.put("versionCode",  appVersion.getVersionCode());
    	r.put("enforcement",  appVersion.getEnforcement());
    	return r;
		
	}

	@RequestMapping("/getImage")
	@IgnoreToken
	public R getImage(HttpServletRequest req) {

		String type = req.getParameter("type");//1/ios  2/安卓
		WfAppImageEntity appImage = appImageService.queryByType(Integer.valueOf(type));

		R r = new R();
		r = r.ok();
		if(appImage != null) {
			r.put("hightresolution", appImage.getHightresolution());
			r.put("lowresolution", appImage.getLowresolution());
			r.put("eventurl", appImage.getEventurl());
			r.put("eventtitle", appImage.getEventtitle());
			r.put("startdate", appImage.getStartdate());
			r.put("enddate", appImage.getEnddate());
			r.put("disable", appImage.getDisable());
			r.put("updatetime", appImage.getUpdatetime());
		}
		return r;

	}
}
